import _Table from "choerodon-ui/pro/lib/table";
import _Form from "choerodon-ui/pro/lib/form";
import _Output from "choerodon-ui/pro/lib/output";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _DataSet from "choerodon-ui/pro/lib/data-set";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _class, _class2;

/**
 * @since 2019-10-10
 */
import React from 'react';
import { Bind } from 'lodash-decorators';
import classNames from 'classnames';
import { Header, Content } from 'components/Page';
import intl from 'utils/intl';
import { CPAAS_IMP } from 'utils/config';
import { TagRender } from 'utils/renderer';
import formatterCollections from 'utils/intl/formatterCollections';
import { getCurrentOrganizationId, getResponse } from 'utils/utils';
import queryString from 'querystring';
import { loadTemplate } from "../../services/importHistoryService";
import styles from "./Detail.less?modules";
var Detail = (_dec = formatterCollections({
  code: ['cimp.importHistory', 'cimp.comment']
}), _dec2 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(Detail, _React$Component);

  var _super = _createSuper(Detail);

  function Detail(props) {
    var _this;

    _classCallCheck(this, Detail);

    _this = _super.call(this, props);
    var _props$location$searc = props.location.search,
        search = _props$location$searc === void 0 ? '' : _props$location$searc;

    var _queryString$parse = queryString.parse(search),
        prefixPatch = _queryString$parse.prefixPatch;

    _this.state = {
      tableDS: null,
      // 动态ds
      prefixPatch: prefixPatch,
      dynamicColumns: [] // 动态列

    };
    return _this;
  }

  _createClass(Detail, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props$match = this.props.match,
          match = _this$props$match === void 0 ? {} : _this$props$match;
      var params = match.params;

      if (params.templateCode) {
        this.getTemplate();
      }
    }
    /**
     * 获取模板(动态列, 客户端前缀)
     */

  }, {
    key: "getTemplate",
    value: function getTemplate() {
      var _this2 = this;

      var _this$props$match2 = this.props.match,
          match = _this$props$match2 === void 0 ? {} : _this$props$match2;
      var _match$params = match.params,
          params = _match$params === void 0 ? {} : _match$params;
      var _this$state$prefixPat = this.state.prefixPatch,
          prefixPatch = _this$state$prefixPat === void 0 ? '' : _this$state$prefixPat;
      loadTemplate({
        code: params.templateCode,
        prefixPatch: prefixPatch
      }).then(function (res) {
        var parsedRes = getResponse(res);

        if (parsedRes) {
          var _res$prefixPatch = res.prefixPatch,
              newPrefixPatch = _res$prefixPatch === void 0 ? '' : _res$prefixPatch,
              templateType = res.templateType,
              _res$templateTargetLi = res.templateTargetList,
              templateTargetList = _res$templateTargetLi === void 0 ? [] : _res$templateTargetLi;

          var _ref = templateTargetList[0] || {},
              _ref$templateLineList = _ref.templateLineList,
              templateLineList = _ref$templateLineList === void 0 ? [] : _ref$templateLineList;

          var dynamicFields = templateLineList.map(function (item) {
            return {
              name: item.columnCode,
              label: item.columnName,
              type: item.columnType !== 'Long' ? item.columnType : 'Number',
              required: item.nullable
            };
          });

          _this2.setState({
            prefixPatch: newPrefixPatch || prefixPatch || (templateType === 'C' ? '' : CPAAS_IMP),
            dynamicColumns: templateLineList.map(function (item, index) {
              if (index !== 0) {
                return {
                  name: item.columnCode
                };
              } else {
                return {
                  name: item.columnCode,
                  width: item.columnName.length * 16 + 64
                };
              }
            }),
            formDS: new _DataSet({
              autoCreate: true,
              autoQuery: true,
              dataKey: 'content',
              fields: [{
                name: 'batch',
                type: 'string',
                label: intl.get('cimp.importHistory.view.title.importBatch').d('导入批次号')
              }, {
                name: 'createdUserName',
                type: 'string',
                label: intl.get('cimp.importHistory.view.title.createUser').d('创建人')
              }, {
                name: 'creationDate',
                type: 'string',
                label: intl.get('cimp.importHistory.view.title.createTime').d('创建时间')
              }],
              transport: {
                read: function read(config) {
                  var _this2$props$match$pa = _this2.props.match.params.importId,
                      importId = _this2$props$match$pa === void 0 ? 0 : _this2$props$match$pa;
                  var url = "".concat(_this2.state.prefixPatch, "/v1/").concat(getCurrentOrganizationId(), "/import/manager/").concat(importId);
                  return _objectSpread(_objectSpread({}, config), {}, {
                    url: url,
                    method: 'GET'
                  });
                }
              }
            }),
            tableDS: new _DataSet({
              autoQuery: true,
              selection: false,
              fields: [].concat(_toConsumableArray(dynamicFields), [{
                name: '_dataStatus',
                type: 'string',
                label: intl.get('cimp.comment.model.comment.dataStatus').d('数据状态')
              }, {
                name: '_info',
                type: 'string',
                label: intl.get('cimp.comment.model.comment.errorMsg').d('错误信息')
              }]),
              transport: {
                read: function read() {
                  var _this2$props$match$pa2 = _this2.props.match.params,
                      _this2$props$match$pa3 = _this2$props$match$pa2.templateCode,
                      templateCode = _this2$props$match$pa3 === void 0 ? '' : _this2$props$match$pa3,
                      _this2$props$match$pa4 = _this2$props$match$pa2.batch,
                      batch = _this2$props$match$pa4 === void 0 ? '' : _this2$props$match$pa4;
                  var url = "".concat(_this2.state.prefixPatch, "/v1/").concat(getCurrentOrganizationId(), "/import/data?templateCode=").concat(templateCode, "&batch=").concat(batch);
                  return {
                    url: url,
                    method: 'GET',
                    transformResponse: function transformResponse(data) {
                      var tempData = JSON.parse(data);
                      if (tempData.content.length === 0) return tempData;
                      tempData.content = tempData.content.map(function (item) {
                        var _item$_batch = item._batch,
                            _batch = _item$_batch === void 0 ? '' : _item$_batch,
                            _item$_dataStatus = item._dataStatus,
                            _dataStatus = _item$_dataStatus === void 0 ? '' : _item$_dataStatus,
                            _item$_dataStatusMean = item._dataStatusMeaning,
                            _dataStatusMeaning = _item$_dataStatusMean === void 0 ? '' : _item$_dataStatusMean,
                            _item$_id = item._id,
                            _id = _item$_id === void 0 ? '' : _item$_id,
                            _item$_templateCode = item._templateCode,
                            _templateCode = _item$_templateCode === void 0 ? '' : _item$_templateCode,
                            _item$_info = item._info,
                            _info = _item$_info === void 0 ? '' : _item$_info;

                        return _objectSpread(_objectSpread({
                          _batch: _batch
                        }, JSON.parse(item._data)), {}, {
                          _dataStatus: _dataStatus,
                          _dataStatusMeaning: _dataStatusMeaning,
                          _id: _id,
                          _templateCode: _templateCode,
                          _info: _info
                        });
                      });
                      return tempData;
                    }
                  };
                }
              }
            })
          });
        }
      });
    }
  }, {
    key: "columns",
    get: function get() {
      return [].concat(_toConsumableArray(this.state.dynamicColumns), [{
        name: '_dataStatus',
        renderer: function renderer(records) {
          var record = records.record;

          var _record$data$_dataSta = record.data._dataStatusMeaning,
              _dataStatusMeaning = _record$data$_dataSta === void 0 ? '' : _record$data$_dataSta;

          var statusList = [{
            status: 'NEW',
            color: 'blue'
          }, {
            status: 'VALID_SUCCESS',
            color: 'green'
          }, {
            status: 'VALID_FAILED',
            color: 'red'
          }, {
            status: 'IMPORT_SUCCESS',
            color: 'green'
          }, {
            status: 'IMPORT_FAILED',
            color: 'red'
          }, {
            status: 'ERROR',
            color: 'red'
          }];
          var tagItem = statusList.find(function (t) {
            return t.status === records.value;
          }) || {};
          return TagRender(records.value, [{
            status: tagItem.status,
            text: _dataStatusMeaning,
            color: tagItem.color
          }]);
        }
      }, {
        name: '_info',
        width: 200
      }]);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state = this.state,
          formDS = _this$state.formDS,
          tableDS = _this$state.tableDS;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cimp.importHistory.view.message.historyDetail').d('历史详情'),
        backPath: "/cimp/history/list"
      }), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement("div", {
        className: classNames(styles['label-col'])
      }, intl.get('cimp.importHistory.view.message.importHistory').d('导入历史')), /*#__PURE__*/React.createElement(_Form, {
        dataSet: formDS,
        columns: 3
      }, /*#__PURE__*/React.createElement(_Output, {
        name: "batch"
      }), /*#__PURE__*/React.createElement(_Output, {
        name: "createdUserName"
      }), /*#__PURE__*/React.createElement(_Output, {
        name: "creationDate"
      })), /*#__PURE__*/React.createElement("div", {
        className: classNames(styles['label-col']),
        style: {
          marginBottom: 20
        }
      }, intl.get('cimp.importHistory.view.message.detailData').d('数据详情')), tableDS !== null ? /*#__PURE__*/React.createElement(_Table, {
        dataSet: tableDS,
        columns: this.columns,
        queryBar: "normal"
      }) : null));
    }
  }]);

  return Detail;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "getTemplate", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "getTemplate"), _class2.prototype)), _class2)) || _class);
export { Detail as default };